package com.ruoyi.bom.service.impl;

import com.ruoyi.bom.domain.SysDept;
import com.ruoyi.bom.service.SysDeptService;
import com.ruoyi.bom.mapper.SysDeptMapper;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.domain.EnabledShopDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 86198
* @description 针对表【sys_dept(部门表)】的数据库操作Service实现
* @createDate 2024-01-18 18:27:21
*/
@Service
public class SysDeptServiceImpl implements SysDeptService{


    @Resource
    private  SysDeptMapper sysDeptMapper;

    @Autowired
    private RedisService redisService;
    @Override
    public List<SysDept> selectDeptList() {
        return sysDeptMapper.selectDeptList();
    }

    @Override
    public  List<EnabledShopDto> getEnabledHllShop() {
        return sysDeptMapper.getEnabledHllShop();
    }

    /**
     * 以金蝶的账号取哗啦啦的账号
     * @param fnumber
     * @return
     */
    public String getEnabledHllShopIdByKingdeeFnumber(String fnumber)
    {
        try {
            Map<String, String> map = redisService.getCacheMap("hllKingShopIdMap");
            if (CollectionUtils.isEmpty(map)) {
                List<EnabledShopDto> list = getEnabledHllShop();
                if (CollectionUtils.isEmpty(list)) return null;
                Map<String, String> map1 = list.stream()
                        .collect(Collectors.toMap(EnabledShopDto::getKingdeeDeptCode, EnabledShopDto::getShopCode));
                //redis持久化
                redisService.setCacheMap("hllKingShopIdMap", map1);
                return map1.get(fnumber);
            } else {

                return map.get(fnumber);
            }
        }catch (Exception ex)
        {
            List<EnabledShopDto> list = getEnabledHllShop();
            if (CollectionUtils.isEmpty(list)) return null;
            Map<String, String> map1 = list.stream()
                    .collect(Collectors.toMap(EnabledShopDto::getKingdeeDeptCode, EnabledShopDto::getShopCode));
            return map1.get(fnumber);
        }

    }

    @Override
    public List<SysDept> selectEnableShop() {
        return sysDeptMapper.selectEnableShop();
    }

    @Override
    public SysDept selectDeptById(Long deptId)
    {
        return sysDeptMapper.selectDeptById(deptId);
    }
}




